package br.com.californiahotel.client.viewControler;

import br.com.californiahotel.client.architecture.viewController.BasicView;
import br.com.californiahotel.client.viewControler.LoginController.Display;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;

public class LoginView extends BasicView implements Display {

	public enum Field{
		LOGIN("LoginField"),OK("OkButton"),PASSWORD("PasswordField");

		private final String fieldName;

		private Field(String name) {
			fieldName = name;
		}

		public String getFieldName() {
			return fieldName;
		}

	}

	private DynamicForm dynamicForm;

	public LoginView() {
		addChild(mountForm());
		setBackgroundImage("praia.jpg");
		setHeight100();
		setWidth100();
	}

	private void addItems() {
		TextItem loginField = new TextItem(Field.LOGIN.getFieldName(),"Login");
		PasswordItem passwordField = new PasswordItem(Field.PASSWORD.getFieldName(),"Senha");
		ButtonItem okButton = new ButtonItem(Field.OK.getFieldName(),"Login");

		loginField.setTitleStyle("titleForm");
		loginField.setShowFocused(false);
		
		passwordField.setTitleStyle("titleForm");
		passwordField.setShowFocused(false);

		okButton.setAlign(Alignment.RIGHT);
		okButton.setStartRow(false);
		okButton.setAutoFit(false);


		dynamicForm.setBorder("3px solid #FFFFFF");
		dynamicForm.setCellPadding(5);
		centralizar();

		dynamicForm.setFields(loginField, passwordField, new SpacerItem(), okButton);
	}

	private void centralizar() {
		dynamicForm.setLeft("50%");
		dynamicForm.setTop("50%");
		int left = dynamicForm.getPageLeft();
		int top = dynamicForm.getPageTop();
		dynamicForm.setLeft(left - dynamicForm.getDefaultWidth() - 20);
		dynamicForm.setTop(top - dynamicForm.getDefaultHeight() - 50);
	}

	@Override
	public DynamicForm getForm() {
		return dynamicForm;
	}

	private DynamicForm mountForm() {
		if (dynamicForm == null) {
			dynamicForm = new DynamicForm();
		}
		addItems();
		return dynamicForm;
	}

}
